package bairui.international.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.project.common.service.Service_Number;
import com.lianjing.sh.web.security.authority.login.Util_Login;
import com.sh_lianjing.state.StateTransition;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.domain.jpa.TB_BranchCompany;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyWithdrawOrder;
import bairui.international.model.status.State_BranchCompanyWithdrawOrder;
import bairui.international.service.Service_BranchCompanyMoneyRecord;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_BranchCompanyWithdrawOrder;
import bairui.international.service.Service_BranchCompanyWithdrawVerifyRecord;

/**
 * 
 * 【(分公司提现单)TB_BranchCompanyWithdrawOrder】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_BranchCompanyWithdrawOrder 模型
 * @see Service_BranchCompanyWithdrawOrder 服务接口
 * @see BService_BranchCompanyWithdrawOrderImpl 服务实现基础
 *
 */
@Service
public class Service_BranchCompanyWithdrawOrderImpl extends BService_BranchCompanyWithdrawOrderImpl implements Service_BranchCompanyWithdrawOrder {

	@Autowired private Service_BranchCompanyWithdrawVerifyRecord service_BranchCompanyWithdrawVerifyRecord;
	@Autowired private Service_BranchCompanyMoneyRecord service_BranchCompanyMoneyRecord;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	@Autowired private Service_Number service_Number;
	private String Number = "BranchCompanyWithdrawOrderNumber";
	
	public TB_BranchCompanyWithdrawOrder createInstance(TB_BranchCompanyStaff branchCompanyStaff, TB_BranchCompany branchCompany, State_BranchCompanyWithdrawOrder state, double amount){
		return dao.save(new TB_BranchCompanyWithdrawOrder(
				new TB_BranchCompanyWithdrawOrder.MustProperties() {
					@Override public String getNumber() {
						return service_Number.generateDateFormatNumber(Number);
					}
					@Override public Double getMoney() {
						return amount;
					}
					@Override public State_BranchCompanyWithdrawOrder getState() {
						return state;
					}
					@Override public TB_BranchCompanyStaff getBranchCompanyStaff() {
						return branchCompanyStaff;
					}
					@Override public TB_BranchCompany getBranchCompany() {
						return branchCompany;
					}
				}, 
				new TB_BranchCompanyWithdrawOrder.OptionalSingleProperties() {
					@Override public Date getSubmitDate() {
						return null;
					}
					@Override public Date getVerifyDate() {
						return null;
					}
					@Override public String getRealName() {
						return null;
					}
					@Override public String getAccountNumber() {
						return null;
					}
					@Override public String getBank() {
						return null;
					}
					@Override public String getVerifyComment() {
						return null;
					}
					@Override public String getRemitImgUrl() {
						return null;
					}
					@Override public TB_Admin getVerifior() {
						return null;
					}
				}
			));
	}
	
	@Override
	public TB_BranchCompanyWithdrawOrder vertifyPass(long id) throws Exception {
		TB_BranchCompanyWithdrawOrder order = findById(id);
		// 更新信息
		StateTransition<State_BranchCompanyWithdrawOrder> trans = order.new UpdateProperties() {
			StateTransition<State_BranchCompanyWithdrawOrder> trans;
			@Override public State_BranchCompanyWithdrawOrder getState(TB_BranchCompanyWithdrawOrder it) throws Exception {
				trans = it.getState().t_verifyPass_from_Verifing();
				return trans.to();
			};
			@Override public Date getVerifyDate(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return new Date();
			};
			@Override public String getVerifyComment(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return null;
			};
			@Override public TB_Admin getVerifior(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return (TB_Admin) Util_Login.getInfo().getUser();
			};
		}.trans;
		// 记录
		service_BranchCompanyWithdrawVerifyRecord.createPassRecord(order, trans.comment(), "");
		//
		return order;
	}

	@Override
	public TB_BranchCompanyWithdrawOrder vertifyNoPass(long id, String message) throws Exception {
		TB_BranchCompanyWithdrawOrder order = findById(id);
		// 更新信息
		StateTransition<State_BranchCompanyWithdrawOrder> trans = order.new UpdateProperties() {
			StateTransition<State_BranchCompanyWithdrawOrder> trans;
			@Override public State_BranchCompanyWithdrawOrder getState(TB_BranchCompanyWithdrawOrder it) throws Exception {
				trans = it.getState().t_verifyNoPass_from_Verifing();
				return trans.to();
			};
			@Override public Date getVerifyDate(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return new Date();
			};
			@Override public String getVerifyComment(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return message;
			};
			@Override public TB_Admin getVerifior(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return (TB_Admin) Util_Login.getInfo().getUser();
			};
		}.trans;
		// 记录
		service_BranchCompanyWithdrawVerifyRecord.createNoPassRecord(order, trans.comment(), message);
		// 金额记录
		service_BranchCompanyMoneyRecord.createReturn(order);
		//
		return order;
	}

	@Override
	public TB_BranchCompanyWithdrawOrder create(TB_BranchCompanyStaff _branchCompanyStaff, Double amount) throws Exception {
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.load(_branchCompanyStaff);
		StateTransition<State_BranchCompanyWithdrawOrder> tras = State_BranchCompanyWithdrawOrder.t_create();
		//
		TB_BranchCompanyWithdrawOrder order = createInstance(branchCompanyStaff, branchCompanyStaff.getBranchCompany(), tras.to(), amount);
		order.new UpdateProperties() {
			@Override public Date getSubmitDate(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return new Date();
			}
			@Override public String getRealName(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return branchCompanyStaff.getUser().getRealName();
			};
			@Override public String getAccountNumber(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return branchCompanyStaff.getUser().getAccountNumber();
			};
			@Override public String getBank(TB_BranchCompanyWithdrawOrder it) throws Exception {
				return branchCompanyStaff.getUser().getBank();
			};
		};
		// 记录
		service_BranchCompanyWithdrawVerifyRecord.createFlowRecord(order, tras.comment());
		// 金额记录
		service_BranchCompanyMoneyRecord.create(order);
		//
		return order;
	}

	@Override
	public void setRemitImgUrl(long id, String remitImgUrl) {
		findById(id).setRemitImgUrl(remitImgUrl);		
	}
	
}
